package com.duoketech.service.weixin.room;

import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.member.Member;
import com.duoketech.model.order.Order;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.weixin.activity.WeiXinActivityService;
import com.duoketech.utils.DateUtils;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

public class WeiXinRoomService {


    public static final WeiXinRoomService ME = new WeiXinRoomService();


    public List<UnitRoom> queryLastestUnitRoom(int limit) {
        String sql = "select * from " + UnitRoom.dao.tableName() + " where status=? order by id desc limit ?";
        return UnitRoom.dao.find(sql, UnitRoom.STATUS_NORMAL, limit);
    }

    public Page<UnitRoom> queryUnitRoom(int pageNum, int pageSize) {
        String sql = " from " + UnitRoom.dao.tableName() + " t1, "+Member.dao.tableName() +" t2  where t1.status=? and t1.create_by = t2.id order by t1.id desc";
        Page<UnitRoom> unitRoomPage = UnitRoom.dao.paginate(pageNum, pageSize, "select t1.*,t2.avatar ", sql, UnitRoom.STATUS_NORMAL);
        for (UnitRoom u:unitRoomPage.getList()) {
            u.set(UnitRoom.PRICE,u.price());
            u.set(UnitRoom.STATUS,u.status());
        }
        return unitRoomPage;
    }


    public Page<RoomOrder> queryUnitRoomOfMine(int pageNum, int pageSize, int loginId) {

        String sql = " from " + RoomOrder.dao.tableName() + " t1, "+UnitRoom.dao.tableName()+" t2 where t1.status<>? and t1.member_id=? and t1.obj_id=t2.id order by t1.id desc";
        Page<RoomOrder> roomOrder = RoomOrder.dao.paginate(pageNum, pageSize, "select t1.*,t2.name,t2.desc", sql, RoomOrder.STATUS_DEL,loginId);
        for(RoomOrder a:roomOrder.getList()){
            a.put(RoomOrder.CREATE_TIME,DateUtils.formateData(a.getDate(Order.CREATE_TIME),DateUtils.YYMMDD_FORMAT));
            a.put("status",WeiXinRoomService.ME.statusConvert(a.getInt(RoomOrder.STATUS)));
        }
        return roomOrder;
    }

    public RoomOrder queryRoomByCardNo(String cardNo, int roomId){
        String sql="select * from "+RoomOrder.dao.tableName()+" where card_no=? and status<>? and obj_id=?";
        return RoomOrder.dao.findFirst(sql,cardNo,RoomOrder.STATUS_DEL,roomId);
    }
    public UnitRoom queryRoomById(int id){
        UnitRoom unitRoom = UnitRoom.dao.findById(id);
        unitRoom.set(UnitRoom.PRICE,unitRoom.price());
        return unitRoom;
    }


    public RoomOrder roomOrderPreview(int id,int memberId) throws ValidateException {
        RoomOrder roomOrder = RoomOrder.dao.findById(id);

        if(roomOrder==null){
            throw new ValidateException("当前订单记录不存在");
        }
        if(roomOrder.getInt(RoomOrder.MEMBER_ID)!=memberId){
            throw new ValidateException("非本人预订订单，无法继续操作");
        }
        UnitRoom room = UnitRoom.dao.findById(roomOrder.getInt(RoomOrder.OBJ_ID));
        roomOrder.put("room",room);
        return roomOrder;
    }

    public String statusConvert(int code){
        return RoomOrder.dao.statusConvert(code);
    }
}
